﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace YAPACS.RisServer.Services.ExtendCells;

/// <summary>
/// 单元格 抽象类
/// </summary>
public abstract class Cell
{
    // 总体宽度
    public static double TotalWidth { get; set; } = 3.8065711559172386;

    public string ClassName { get; set; } // css class

    public string Type { get; set; } = "container";

    public bool IsFixedHeight { get; set; } = true;

    public double originalWidth { get; set; } = 0;

    public string Width { get; set; }

    public int Height { get; set; } = 35;// 当行单元格高度 

    public int RowSpan { get; set; } = 1;

    [JsonProperty(PropertyName = "Style")]
    public JObject Style { get; set; }   // amis内部样式

    [JsonProperty(PropertyName = "Body")]
    public JObject Body { get; set; }

    /// <summary>
    /// 计算宽度比例，修改样式中宽度
    /// </summary>
    public virtual void WidthCoculate()
    {
        Style ??= [];
        const double totalWeight = 3.8065711559172386;
        float widthPercent = (float)Math.Round((originalWidth / totalWeight) * 100, 2);
        if (Style["width"] != null)
        {
            Style.Remove("width");       
        }
        Style.Add("width", widthPercent + "%");
        Width = widthPercent + "%";
    }


    /// <summary>
    /// 计算并添加高度样式
    /// </summary>
    public virtual void HeightCoculate()
    {
        Style ??= [];
        Style.Add("height", (Height * RowSpan).ToString() + "px");
    }

    /// <summary>
    /// 获取单元格schema
    /// </summary>
    /// <returns></returns>
    public virtual JObject GetSchema()
    {
        JObject container = [];
        if (!string.IsNullOrEmpty(ClassName))
        {
            container.Add("className", ClassName);
        }
        container.Add("type", Type);
        container.Add("isFixedHeight", IsFixedHeight);
        JObject wrapperCustomStyle = new JObject();
        JObject root = new JObject();
        root.Add("margin-top", "4px");
        wrapperCustomStyle.Add("root", root);
        container.Add("wrapperCustomStyle", wrapperCustomStyle);
        if (Body != null)
        {
            container.Add("body", Body);
        }

        if (Style != null)
        {
            container.Add("style", Style);
        }

        return container;
    }

}
